local xiandeng = fk.CreateSkill{
  name = "wk_heg__xiandeng",
}

Fk:loadTranslationTable{
  ["wk_heg__xiandeng"] = "先登",
  [":wk_heg__xiandeng"] = "当你造成伤害后，你可以明置一张手牌；准备阶段，你可以弃置至少你体力值数张明置牌，然后获得一个“先驱”标记。",
  ["#wk_heg__xiandeng-show"] = "先登：你可以明置一张手牌",
  ["#wk_heg__xiandeng-dis"] = "先登：你可以弃置至少 %dest 张明置牌，然后获得“先驱”",
}

local H = require "packages.ol_hegemony.util"
local HG = require "packages.new_heg_diy_my.util"

xiandeng:addEffect(fk.Damage, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(xiandeng.name) and player == target and table.find(player:getCardIds("h"), function (id)
      return Fk:getCardById(id):getMark(MarkEnum.ShownCards) == 0 end)
  end,
  on_cost = function (self, event, target, player, data)
    local notshowns = table.filter(player:getCardIds("h"), function (id)
      return Fk:getCardById(id):getMark(MarkEnum.ShownCards) == 0 end)
    local pat = ".|.|.|.|.|.|"..table.concat(notshowns, ",")
    local cards = player.room:askToCards(player,{
      max_num = 1,
      min_num = 1,
      include_equip = false,
      skill_name = xiandeng.name, 
      pattern = pat,
      prompt = "#wk_heg__xiandeng-show"})
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    HG.showCards(player, event:getCostData(self))
  end
})

xiandeng:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player == target and player.phase == Player.Start and #HG.getShownCards(player) >= player.hp
  end,
  on_cost = function (self, event, target, player, data)
    local showns = HG.getShownCards(player)
    if #showns == player.hp then
      event:setCostData(self, showns)
      return player.room:askToSkillInvoke(player, {
        skill_name = xiandeng.name
      })
    end
    local pat = ".|.|.|.|.|.|"..table.concat(showns, ",")
    local cards = player.room:askToCards(player,{ 
      min_num = player.hp, 
      max_num = 998,
      include_equip = false,
      skill_name = xiandeng.name,
      pattern =  pat,
      prompt = "#wk_heg__xiandeng-dis::"..player.hp})
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self), xiandeng.name, player)
    H.addHegMark(room, player, "vanguard")
  end
})

return xiandeng